package log;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public class main {
    public static void main(String[] args) {
        List<UserAccessLog> logs = Arrays.asList(
                new UserAccessLog("u1", "张三", "/home", LocalDateTime.of(2025, 7, 1, 10, 0)),
                new UserAccessLog("u1", "张三", "/product", LocalDateTime.of(2025, 7, 3, 14, 0)),
                new UserAccessLog("u2", "李四", "/home", LocalDateTime.of(2025, 6, 28, 16, 0)), // 超过 7 天，应被过滤
                new UserAccessLog("u2", "李四", "/cart", LocalDateTime.of(2025, 7, 5, 9, 0)),
                new UserAccessLog("u3", "王五", "/home", LocalDateTime.of(2025, 7, 6, 11, 0))
        );

        // Step 1: 过滤出最近 7 天的日志
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime sevenDaysAgo = now.minus(7, ChronoUnit.DAYS);

        List<UserAccessLog> recentLogs = logs.stream()
                .filter(log -> !log.getAccessTime().isBefore(sevenDaysAgo)).collect(Collectors.toList());
        System.out.println("最近 7 天的日志：" + recentLogs);
        // Step 2: 按用户 ID 分组
        Map<String, List<UserAccessLog>> logsByUser = recentLogs.stream().collect(Collectors.groupingBy(UserAccessLog::getUserId));
        System.out.println("按用户 ID 分组：" + logsByUser);
        // Step 3: 对分组数据进行聚合统计
        List<UserVisitSummary> summaries = logsByUser.entrySet().stream()
                .map(entry -> {
                    String userId = entry.getKey();
                    List<UserAccessLog> userLogs = entry.getValue();

                    int count = userLogs.size();
                    LocalDateTime firstVisit = userLogs.stream()
                            .map(UserAccessLog::getAccessTime)
                            .min(LocalDateTime::compareTo)
                            .orElse(null);
                    LocalDateTime lastVisit = userLogs.stream()
                            .map(UserAccessLog::getAccessTime)
                            .max(LocalDateTime::compareTo)
                            .orElse(null);
                    String username = userLogs.get(0).getUsername(); // 所有记录用户名一致

                    return new UserVisitSummary(userId, username, count, firstVisit, lastVisit);
                })
                .filter(summary -> summary.getTotalVisits() > 0) // 可选：排除空数据
                .collect(Collectors.toList());

        // Step 4: 按总访问次数从高到低排序
        List<UserVisitSummary> sortedSummaries = summaries.stream()
                .sorted((a, b) -> Integer.compare(b.getTotalVisits(), a.getTotalVisits()))
                .collect(Collectors.toList());

        // Step 5: 转成 Map<userId, UserVisitSummary>
        sortedSummaries.stream()
                .collect(Collectors.toMap(
                        UserVisitSummary::getUserId,
                        summary -> summary,
                        (existing, replacement) -> existing// 防止 Key 冲突
                        //保持排序顺序
                ));
    }




}

